const app = getApp();
// const controllers = app.controllers;
const controllers = require("../../../controllers/ControllerVip").controller;
const controller = require("../../../controllers/Controller").controller

Page({
  data: {
    dataInfo: {},
    id: null,
    shopId: null,
    shopDetails: "",
    selectIndex: '',
    showModalStatus: false,
    staffList: [],
    selectIndex: "",
    staffId: "",
    staffName: ""
  },
  onLoad: function (options) {
    let id = options.id;
    let shopId = app.globalData.shopId;
    if (options.notQC) {
      this.setData({
        notQC: options.notQC
      })
    }
    if (options.shopId) {
      shopId = options.shopId;
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene);
      let vipId = scene.split(",")[0].split(":")[1];
      let sId = scene.split(",")[1].split(":")[1];
      if (vipId) {
        id = vipId;
      }
      if (sId) {
        shopId = sId;
      }
    }
    if (shopId) {
      wx.setStorageSync('shopId', shopId);
      app.globalData.shopId = shopId;
    }
    this.setData({
      'id': id,
      shopId: shopId
    }); // || 28
  },
  onShow() {
    if (wx.getStorageSync('usertoken')) {
      this.getInfo(this.data.id);
      this.getStaffList()
      this.getShopDetail()
    } else {
      app.usertokenReadyCallback = res => {
        if (res.data.sessionId) {
          this.getInfo(this.data.id);
          this.getShopDetail()
          this.getStaffList()

        }
      }
    }

  },
  getInfo: function (id) {
    let d = {
      vipId: this.data.id,
      shopId: this.data.shopId
    }
    let that = this;
    controllers.getVipDetail2(d).then(res => {
      that.setData({
        dataInfo: res.data
      })
    });
  },

  //返回店铺首页
  toShopIndex() {
    wx.reLaunch({
      url: "/pages/Index/Index?shopId=" + this.data.shopId,
    })
  },
  //获取员工列表
  getStaffList() {
    let shopId = wx.getStorageSync('shopId')
    controller.getStaffList({
      shopId: shopId
    }).then(res => {
      res.data.unshift({
        id: ''
      })
      this.setData({
        staffList: res.data
      })
    })
  },

  isReceiveCard() {
    wx.showModal({
      title: "提示",
      content: "是否立即领取?",
      success(res) {
        if (res.confirm) {
          that.receiveCard()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //领取亲友卡
  receiveCard() {
    let vipId = this.data.id;
    let value = {
      vipId: vipId
    }
    controller.getByQrCode(value).then(res => {
      wx.showToast({
        title: '领取成功',
      })
    })
  },
  // 打开弹窗
  isOpenCard() {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否开卡？',
      success(res) {
        if (res.confirm) {
          that.setData({
            showModalStatus: true
          })
        }
      }
    })
  },
  //选择员工
  selectStaff(e) {
    let index = e.currentTarget.dataset.index
    console.log(e)
    if (index === this.data.selectIndex) {
      this.setData({
        selectIndex: "",
        staffId: "",
        staffName: ""
      })
    } else {
      this.setData({
        selectIndex: index,
        staffId: this.data.staffList[index].id,
        staffName: this.data.staffList[index].nick
      })
    }
  },

  // //确定员工
  // defineStaff(){
  //   if(this.data.selectIndex||this.data.selectIndex===0){
  //     this.setData({
  //       staffId:this.data.list[this.data.selectIndex].id,
  //       staffName:this.data.list[this.data.selectIndex].nick
  //     })
  //   }
  // },
  // 隐藏弹框
  hideModal: function () {
    this.setData({
      showModalStatus: false,
      selectIndex: "",
      staffId: "",
      staffName: ""
    })
  },
  //跳转员工详情
  selectStaffDetail(e) {
    let index = e.currentTarget.dataset.index
    if (index == 0) return
    let value = this.data.staffList[index]
    wx.navigateTo({
      url: '/pages/selecStaffDetail/selecStaffDetail?staffDetail=' + encodeURIComponent(JSON.stringify(value)),
    })
  },

  //会员开卡
  OpenCard() {
    let that = this
    let shopId = this.data.shopId;
    let vipId = this.data.id;
    let shopAccountId = this.data.staffId;
    let selectIndex = this.data.selectIndex;

    if (!(selectIndex || selectIndex === 0)) {
      wx.showToast({
        title: '请正确选择跟单员工',
        icon: 'none'
      })
      return
    }
    this.hideModal()
    controllers.openCard({
      shopId: shopId,
      vipId: vipId,
      shopAccountId
    }).then(res => {
      if (res.code == 1) {
        let orderNo = res.data.orderNo;
        let openId = app.globalData.openId
        controllers.payMent({
          shopId: shopId,
          orderNo: orderNo,
          openId: openId,
          frontUrl: "mp:///pages/Index/Index"
        }).then(res => {
          if (res.code == 1) {
            let data = JSON.parse(res.data);
            console.log(data)
            wx.requestPayment({
              nonceStr: data.nonceStr,
              package: data.package,
              paySign: data.paySign,
              timeStamp: data.timeStamp,
              signType: data.signType,
              success(res) {
                wx.showToast({
                  title: '购买成功',
                  duration: 2000
                });
                console.info("##########", res);
                wx.navigateTo({
                  url: '/pages/my/vip/vip_myCard/index'
                })

              },
              fail(res) {
                console.info(res);
                if (res.errMsg && res.errMsg.indexOf("cancel") == -1) {
                  wx.showToast({
                    icon: "none",
                    title: '购买失败' + res.errMsg,
                    duration: 2000
                  })
                }

              }
            });
          }
        })
      } else {
        wx.showToast({
          icon: "none",
          title: '开卡失败',
          duration: 2000
        })
      }
    })

  },

  //跳转优惠券详情
  toDetail(e) {
    let id = ""
    if (this.data.receiveId) {
      id = e.currentTarget.dataset.goodsid
    } else {
      id = e.currentTarget.dataset.id
    }
    let couponAccountId = e.currentTarget.dataset.haveid || ''
    let couponType = e.currentTarget.dataset.paytype
    let couponBagId = e.currentTarget.dataset.couponbagid
    let shopId = e.currentTarget.dataset.shopid
    if (this.data.dataInfo.vip.type == 4) {
      wx.navigateTo({
        url: '/pages/Mall/couponDetailBuy?isOnlyShow=1&id=' + id + "&couponAccountId=" + couponAccountId + '&couponBagId=' + couponBagId + "&out=1" + "&shopId=" + shopId,
      })
    } else {
      wx.navigateTo({
        url: '/pages/Mall/Coupondetail?isOnlyShow=1&id=' + id + "&couponAccountId=" + couponAccountId,
      })
    }
  },

  toUrl(e) {
    var d = e.currentTarget.dataset;
    let url = d.url;
    console.info(url);
    wx.navigateTo({
      url: url
    });
  },
  onBack() {
    wx.navigateBack();
  },

  //获取店铺信息
  getShopDetail() {
    let shopId = wx.getStorageSync('shopId')
    controller.shopDetails({
      shopId: shopId
    }).then(res => {
      this.setData({
        shopDetails: res.data
      })
    })
  }

})